package cn.web.demo.controller;

import cn.web.demo.model.CouresResult;
import cn.web.demo.model.CourseOrder;
import cn.web.demo.model.PageResult;
import cn.web.demo.model.Role;
import cn.web.demo.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RoleService roleService;


    @RequestMapping("/list")
    public String list(){
        return "/role/list";
    }

    @RequestMapping("/listJson")
    @ResponseBody
    public PageResult listJson(){
        PageResult<Role> pageResult=new PageResult<>();
        pageResult.setData(roleService.findAllRoles());
        return pageResult;
    }

    @RequestMapping("/add")
    public String add(){
        return "/role/add";
    }

    @RequestMapping("save")
    @ResponseBody
    public CouresResult save(Role role,String ids){
        roleService.save(role, ids);
        return new CouresResult();
    }

    @RequestMapping("/edit")
    public String edit(Model model,Integer id){
       Role role=roleService.FindByRoleId(id);
       model.addAttribute("role",role);
       model.addAttribute("ids",roleService.findPermissionIdsByRoleId(id));
       //System.out.println(roleService.findPermissionIdsByRoleId(id));
        return "/role/edit";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public CouresResult delete(Integer order_id){
        roleService.findRolePermissionDelt(order_id);
        return new CouresResult();
    }


}
